package com.example.controller;

import com.example.domain.dto.CustGetDTO;
import com.example.domain.dto.ExportDTO;
import com.example.domain.po.CustInfo;
import com.example.domain.vo.CustGetVO;
import com.example.domain.vo.ExportVO;
import com.example.service.CustInfoService;
import com.example.service.CustRecordsService;
import com.example.service.FileUploadService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class CustController {
    @Resource
    private CustInfoService custInfoService;

    @Resource
    private FileUploadService fileUploadService;

    @Resource
    private CustRecordsService custRecordsService;

    @PostMapping("/get")
    public CustGetVO getInfo(@RequestBody CustGetDTO custGetDTO){
        long startTime = System.currentTimeMillis();
        if(ObjectUtils.isEmpty(custGetDTO.getCustId()) && ObjectUtils.isEmpty(custGetDTO.getMobile())){
            return null;
        }
        CustInfo custInfo = custInfoService.CacheCustInfo(custGetDTO);
        CustGetVO info = custInfoService.getInfo(custInfo, custGetDTO);
        long middleTime = System.currentTimeMillis();
//        System.out.println("查询mid耗时：" + (middleTime - startTime));
        // 营销记录+1
        custRecordsService.insertCustRecord(info.getCustId());
        long endTime = System.currentTimeMillis();
        Double time = 1.0 * (endTime - startTime);
//        System.out.println("查询+插入耗时：" + (endTime - startTime));
        info.setTotalTime(time);
        return info;
    }
    @PostMapping("/export")
    public ExportVO export(@RequestBody ExportDTO exportDTO, HttpServletResponse response){
        Double time = fileUploadService.build(exportDTO,response);
        return new ExportVO("/download/custInfo.zip", time);
    }

    @PostMapping("/update")
    public boolean update(@RequestBody CustInfo custInfo){
        if (ObjectUtils.isEmpty(custInfo) || ObjectUtils.isEmpty(custInfo.getId())){
            return false;
        }
        return custInfoService.updateById(custInfo);
    }
}
